package com.zf.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zf.admin.pojo.*;
import com.zf.admin.service.ApplyService;
import com.zf.admin.service.HouselistService;
import com.zf.admin.service.UserlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ApplyController {
    @Autowired
    private UserlistService userlistService;
    @Autowired
    private HouselistService houselistService;
    @Autowired
    private ApplyService applyService;

    //管理员查看申请看房列表
    @RequestMapping("/findapplylist")
    public String findapplylist(Model model, @RequestParam(required = false, defaultValue = "1") Integer page,
                                @RequestParam(required = false, defaultValue = "2") Integer pageSize) throws Exception {
        PageHelper.startPage(page, pageSize);
        List<Apply> applylist = applyService.findapplylist();
        PageInfo<Apply> p = new PageInfo<Apply>(applylist);
        model.addAttribute("applylist", applylist);
        model.addAttribute("p", p);
        model.addAttribute("mainPage", "applylist.jsp");
        return "admin/main1";
    }

    @RequestMapping("/applychangehousestatus")
    public String applychangehousestatus(HttpSession httpSession, Model model, String house_id) throws Exception {
        User user1 = (User) httpSession.getAttribute("user");
        Integer user_id = user1.getId();
        Userlist userlist = userlistService.findhasuserlist(user_id);
        Houselist houselist = houselistService.findhouseid(house_id);
        houselist.setStatus("已租赁");
        houselistService.updatehousestatus(houselist);
        Zulist zulist = new Zulist();
        zulist.setHouse_id(house_id);
        zulist.setPrice(houselist.getPrice());
        zulist.setAddress(houselist.getAddress());

        return "";
    }

    //管理员拒绝看房申请
    @RequestMapping("/refuseapply")
    public String refuseapply(String house_id, Model model) {
        Houselist houselist = new Houselist();
        houselist.setHouseid(house_id);
        houselist.setStatus("未租赁");
        applyService.refuseapply(houselist);

        return "redirect:findapplylist";
    }

}
